<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        .container {
            width: 350px;
            height: 300px;
            margin: 0 auto;
            /* border: 1px black solid; */
            text-align: center;
        }

        .grey {
            color: grey;
        }

        .container .row {
            width: 350px;
            height: 40px;

            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .container .row input {
            width: 260px;
            height: 30px;
        }

        #sub {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>表白墙</h1>
        <p class="grey">输入后点击提交, 会将信息显示下方空白处</p>
        <div class="row">
            <span>谁:</span> <input type="text" name="" id="from">
        </div>
        <div class="row">
            <span>对谁:</span> <input type="text" name="" id="to">
        </div>
        <div class="row">
            <span>说什么:</span> <input type="text" name="" id="message">
        </div>
        <input type="button" value="提交" id="sub" onclick="submit()">      
    </div>

    <script src="js/jquery.min.js"></script>
   <script>
        $.ajax({
            url:"/message/get",
            type:"get",
            success:function(messageInfos){
                var finalHtml = "";
                for(var message of messageInfos){
                finalHtml+=('<div>'+message.from+' 对 '+message.to+' 说 '+message.message+'</div>');
                }
            $(".container").append(finalHtml);
            }   
        }); 
        function submit(){
            //获取留言的内容
            var from = $('#from').val();
            var to = $('#to').val();
            var message = $('#message').val();
            if (from== '' || to == '' || message == '') {
                return;
            }
            //发送ajax请求
            $.ajax({
                url:"/message/publish",
                type:"post",
                data:{
                    from:$('#from').val(),
                    to:$('#to').val(),
                    message:$('#message').val()
                },
                success:function(result){
                    if(result){
                        var divE = ("<div>"+from +"对" + to + "说:" + message+"</div>");                
                        $(".container").append(divE);            
                        $('#from').val("");
                        $('#to').val("");
                        $('#message').val("");   
                    }else{
                        alert("输入不合法");
                    }
                }
            });
        }
   </script>
</body>
</html>

